←Select platform

ReplaceIntersectedCharacters(IEnumerable<LeadRect>,char,int) Method

Summary

Replaces the characters intersecting with the specified rectangles at the specified percentage.

Syntax
C#
Objective-C
C++/CLI
Python
public bool ReplaceIntersectedCharacters( 
   IEnumerable<LeadRect> bounds, 
   char replaceCharacter, 
   int intersectionPercentage 
) 
- (BOOL)replaceIntersectedCharacters:(NSArray<NSValue *> *)bounds withCharacter:(char)replaceCharacter intersectionPercentage:(NSInteger)intersectionPercentage; 
public:  
   bool ReplaceIntersectedCharacters( 
      IEnumerable<LeadRect^>^ bounds, 
      Char replaceCharacter, 
      Int32 intersectionPercentage 
   ) 
def ReplaceIntersectedCharacters(self,bounds,replaceCharacter,intersectionPercentage): 

Parameters

bounds

List of rectangles containing the bounds to check. These values are in pixels.

replaceCharacter

The new character code to use. Use 0 to delete the characters.

intersectionPercentage

Intersection percentage between each character and bounds.

Return Value

true if one or more characters were replaced; otherwise, false.

Remarks

This is a helper method used to quickly perform actions such as redacting or removing characters from the recognition data based on their locations.

intersectionPercentage can be used to control the percentage of intersection on which to perform the operation as follows:

  • Value of 1: Means only perform the operation if 1% or more of the character boundary intersects with any of the items in bounds.
  • Value of 50: Means only perform the operation if 50% or more of the character boundary intersects with any of the items in bounds.
  • Value of 100: Means only perform the operation if 100% of the character boundary intersects with any of the items in bounds.

intersectionPercentage value of 0 means use the default percentage of 50%.

Example
C#
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Ocr; 
using Leadtools.Forms.Common; 
using Leadtools.Document.Writer; 
using Leadtools.WinForms; 
using Leadtools.Drawing; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Color; 
 
public void OcrAndRedactToPdf() 
{ 
   string tifFileName = Path.Combine(LEAD_VARS.ImagesDir, "OCR1.tif"); 
   string pdfFileNameBefore = Path.Combine(LEAD_VARS.ImagesDir, "OCR-Redact-Before.pdf"); 
   string pdfFileNameAfter = Path.Combine(LEAD_VARS.ImagesDir, "OCR-Redact-After.pdf"); 
 
   // Create OCR engine 
   using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD)) 
   { 
      ocrEngine.Startup(null, null, null, LEAD_VARS.OcrLEADRuntimeDir); 
 
      // Load the OCR1.tif image and recognize it 
      RasterImage image = ocrEngine.RasterCodecsInstance.Load(tifFileName, 1); 
      using (IOcrPage ocrPage = ocrEngine.CreatePage(image, OcrImageSharingMode.AutoDispose)) 
      { 
         ocrPage.Recognize(null); 
 
         // Save it, note that all the data is in the result PDF file, including "Fast Facts" 
         using (IOcrDocument ocrDocument = ocrEngine.DocumentManager.CreateDocument(null, OcrCreateDocumentOptions.AutoDeleteFile)) 
         { 
            ocrDocument.Pages.Add(ocrPage); 
            ocrDocument.Save(pdfFileNameBefore, DocumentFormat.Pdf, null); 
         } 
 
         // Now, redact "Fast Facts", this could be an input from the user based on rubber banding or Regex. 
         LeadRect redactRect = LeadRect.FromLTRB(300, 800, 730, 900); 
         char replaceCharacter = '*'; 
         IOcrPageCharacters ocrPageCharacters = ocrPage.GetRecognizedCharacters(); 
         ocrPageCharacters.ReplaceIntersectedCharacters(new LeadRect[] { redactRect }, replaceCharacter); 
         ocrPage.SetRecognizedCharacters(ocrPageCharacters); 
 
         // Save it again, note that "Fast Facts" is replaced with * characters 
         using (IOcrDocument ocrDocument = ocrEngine.DocumentManager.CreateDocument(null, OcrCreateDocumentOptions.AutoDeleteFile)) 
         { 
            ocrDocument.Pages.Add(ocrPage); 
            ocrDocument.Save(pdfFileNameAfter, DocumentFormat.Pdf, null); 
         } 
      } 
   } 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS22\Resources\Images"; 
   public const string OcrLEADRuntimeDir = @"C:\LEADTOOLS22\Bin\Common\OcrLEADRuntime"; 
} 
Requirements

Target Platforms

Help Version 22.0.2023.3.31
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Ocr Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.